/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.Color;
import java.awt.Font;
import java.util.List;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.OSDPrinter;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.expression.dcExpression;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.Variable;
 
/**
 *
 * @author Tal Eisenberg
 */
public class OSD extends Module {
    
    List<Object[]> triggerDisplay;
    Font font;
    Color color;
    long duration;
    OSDPrinter printer;

    public void setTriggerDisplay(List<Object[]> value, SetProperty.Flag flag) {
        if (flag == SetProperty.Flag.ADD)
            triggerDisplay.addAll(value);
        else triggerDisplay = value;
    } public List<Object[]> getTriggerDisplay() { return triggerDisplay; }
    
    public void setFont(Font value) {
        font = value;
    } public Font getFont() { return font; }
    
    public void setColor(Color value) {
        color = value;
    } public Color getColor() { return color; }
    
    public void setDuration(Long value) {
        duration = value;
    } public long getDuration() { return duration; }
    
    public void commandPrint(List<Variable> parameters) {
        //syntax: (print)text,duration
        if (parameters==null || parameters.isEmpty()) {
            log("Missing first print command parameter (display)", MSG.WARNING);
            return;
        }
        if (parameters.size()>1 && !parameters.get(1).getDataType().matches(DataType.LONG)) {
            log("Invalid type for 2nd command parameter (duration), a long variable is required.", MSG.WARNING);
            return;
        }
        String text = parameters.get(0).getValue(this).toString();

        try {
            if (printer!=null) printer.close();
            printer = new OSDPrinter(font, text, duration, color);
        } catch (Exception e) { log(e.getMessage(), MSG.WARNING);}
        
    }
    
    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        this.setInVars(in, timestamp, channel);
        for (Object[] item : triggerDisplay) {
            VarMatcher trigger = (VarMatcher)item[0];
            if (trigger.matches(in, channel, this)) {
                dcExpression expr = (dcExpression)item[1];
                try {
                    if (printer!=null) printer.close();
                    printer = new OSDPrinter(font, (String)expr.evaluate(this), duration, color);
                } catch (Exception e) { 
                    log("While running the osdprinter: " + e.getMessage(), MSG.WARNING);
                }

            }
        }
    }

}
